package com.woniuxy.huayoung.service.impl;

import com.woniuxy.huayoung.entity.TravelGoods;
import com.woniuxy.huayoung.mapper.TravelGoodsMapper;
import com.woniuxy.huayoung.service.TravelGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TravelGoodsImpl implements TravelGoodsService {

    private final Logger LOGGER = LoggerFactory.getLogger(TravelGoodsImpl.class);

    @Resource
    private TravelGoodsMapper travelGoodsMapper;


    @Resource
    private RedisTemplate<String,Object> redisTemplate;


//根据出发地/目的地/商品类别搜索旅游商品
    @Override
    public List<TravelGoods> findByTravelGoods(TravelGoods travelGoods) {
        List<TravelGoods> travelGoodsList = travelGoodsMapper.findByTravelGoods(travelGoods);
        return travelGoodsList;
    }

    //根据出发地/目的地/商品类别/旅游日期搜索旅游商品
    @Override
    public List<TravelGoods> findByTravelGoods1(TravelGoods travelGoods) {
        List<TravelGoods> travelGoodsList = travelGoodsMapper.findByTravelGoods1(travelGoods);
        return travelGoodsList;
    }

//根据出发地/目的地/商品类别/旅游日期/商品价格搜索旅游商品
    @Override
    public List<TravelGoods> findByTravelGoods2(TravelGoods travelGoods) {
        List<TravelGoods> travelGoodsList = travelGoodsMapper.findByTravelGoods2(travelGoods);
        return travelGoodsList;
    }


//根据出发地/目的地/商品类别/旅游日期/商品价格搜索旅游商品(当商品价格大于6000时)
    @Override
    public List<TravelGoods> findByTravelGoods3(TravelGoods travelGoods) {
        List<TravelGoods> travelGoodsList = travelGoodsMapper.findByTravelGoods3(travelGoods);
        return travelGoodsList;
    }


//根据商品id查询相应商品的详情
    @Override
    public TravelGoods findGoodsById(int id) {
        TravelGoods travelGoods = travelGoodsMapper.findGoodsById(id);
        return travelGoods;
    }

//根据商品id查询到商品的库存量
    @Override
    public Integer findGoodsStoreNum(Integer id) {
        LOGGER.debug("查询sql库中的商品库存量");
        return travelGoodsMapper.findGoodsStoreNum(id);
    }

//实现用户点击加减号，redis中的商品库存量相应更改的操作
    @Override
    public Boolean choosegoodsnum(Integer id, Integer goodsnum){
        //根据id查询到相应商品库存量
        Integer goodsStoreNum = this.findGoodsStoreNum(id);

        //判断库存量和用户将要购买的商品数量的大小
        if (goodsStoreNum.intValue() < goodsnum.intValue()){
            //库存量不足，解决超卖问题
            return false;
        } else {
            //库存量足够
                //改变redis缓存中以及数据库中的缓存量
            redisTemplate.opsForValue().set("goodsStoreNum",goodsStoreNum-goodsnum);
            LOGGER.debug("更改redis缓存成功");
            return true;
        }
    }

//    @Override
//    public Integer updateNum(Integer id, Integer goodsnum) {
//
//    }

    @Override
    public TravelGoods findByid(int id) {
        return travelGoodsMapper.findByid(id);
    }
}
